package com.shopping.www.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shopping.www.model.*;
import com.shopping.www.service.BusinessService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/business/*")
public class BusinessServlet extends BaseServlet {
    private BusinessService service=new BusinessService();
    //查询货架商品
  public void selectShelveBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String business_id=request.getParameter("business_id");
      business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
      int _business_id=Integer.parseInt(business_id);
      BusinessBrand<Brand> businessBrand= service.selectShelveBrands(_business_id);
      String jsonString = JSON.toJSONString(businessBrand, SerializerFeature.IgnoreErrorGetter);
      //将字符串响应到页面上
      response.setContentType("text/json;charset=utf-8");
      response.getWriter().write(jsonString);
  }
  //下架
    public void leaveFromShelve(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _brand_id=Integer.parseInt(brand_id);
        service.leaveFromShelve(_business_id,_brand_id);
    }
    //查询仓库商品
    public void selectWareBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        BusinessBrand<Brand> businessBrand= service.selectWareBrands(_business_id);
        String jsonString = JSON.toJSONString(businessBrand, SerializerFeature.IgnoreErrorGetter);
        //将字符串响应到页面上
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //上架
    public void PutOnShelve(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _brand_id=Integer.parseInt(brand_id);
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        String num=request.getParameter("num");
        num=new String(num.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _num=Integer.parseInt(num);
        String price=request.getParameter("price");
        price=new String(price.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _price=Integer.parseInt(price);
        String belong=request.getParameter("belong");
        belong=new String(belong.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        String description=request.getParameter("description");
        description=new String(description.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        service.PutOnShelve(_brand_id,_num,_price,belong,description,_business_id);
    }
    //引入商品
    public void importBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        String businessName=request.getParameter("businessName");
        businessName=new String(businessName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type","application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","POST");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
        BufferedReader br=request.getReader();
        String params=br.readLine();
        ImportBrand importBrand=JSON.parseObject(params,ImportBrand.class);
        Boolean b= service.importBrand(_business_id,businessName,importBrand);
        if(b){
            response.getWriter().write("success");
        }else{
            response.getWriter().write("fail");
        }
    }
    //申请解封
    public void applyLiftBan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id=request.getParameter("brand_id");
        brand_id=new String(brand_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _brand_id=Integer.parseInt(brand_id);
        String liftBanApply=request.getParameter("liftBanApply");
        liftBanApply=new String(liftBanApply.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        service.applyLiftBan(_brand_id,liftBanApply);
  }
  //查询用户点击配送的商品订单
    public void selectBusinessDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        BrandAndNum<Brand> brandAndNum=service.selectBusinessDelivery(_business_id);
        String jsonString=JSON.toJSONString(brandAndNum,SerializerFeature.DisableCircularReferenceDetect);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //为商品呼叫骑手
    public void callDelivery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String deliveryId=request.getParameter("deliveryId");
        deliveryId=new String(deliveryId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _deliveryId=Integer.parseInt(deliveryId);
        service.callDelivery(_deliveryId);
  }
  //在仓库中删除商品删除前检查商品是否在货架
  public void checkDeleteWare(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      request.setCharacterEncoding("utf-8");
      response.setHeader("content-type","application:json;charset=utf8");
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Methods","POST");
      response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
      BufferedReader br=request.getReader();
      String params=br.readLine();
      int[] selectedIds=JSON.parseObject(params,int[].class);
      String business_id=request.getParameter("business_id");
      business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
      int _business_id=Integer.parseInt(business_id);
      Boolean b=service.checkDeleteWare(selectedIds,_business_id);
      if(b){
          response.getWriter().write("success");
      }else{
          response.getWriter().write("fail");
      }
  }
  //删除商品
    public void deleteWare(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type","application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods","POST");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
        BufferedReader br=request.getReader();
        String params=br.readLine();
        int[] selectedIds=JSON.parseObject(params,int[].class);
        String business_id=request.getParameter("business_id");
        business_id=new String(business_id.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        int _business_id=Integer.parseInt(business_id);
        service.deleteWare(selectedIds,_business_id);
    }

  }
